
const app = getApp()

Page({
    data: {
        addressStatus: 2,
        scrollTop: '',
        keepList: [],
        loading:false,
        status:1
    },

    onLoad: function (options) {
        this.loadAllStore()
    },
    /**
     * 加载全部店铺
     */ 
    loadAllStore() {
        let { page = 1, total = 2, loading = false, list = [] } = this.data
        if (page > total || loading) {
            return
        }
        wx.showLoading({
            title: '加载中...',
        })
        this.setData({ loading: true })
        app.http.get('index/store', {
            data: {
                page,
                spreadId: this.data.spreadId ? this.data.spreadId : ''
            }
        }).then(res => {
            page++
            list.push(...res.data.item)
            total=res.data.total
            this.setData({
                banner: res.data.banner,
                list,
                total,
                loading: false,
                page,
            })
            page++
            loading: false
            total
            wx.setNavigationBarTitle({
                title: wx.getStorageSync('store'),
            })
            wx.hideLoading()
            // wx.stopPullDownRefresh()
            this.loadKeepStore()
        })
    },
    /**
     * 加载收藏的店铺
    */
    loadKeepStore() {
        if ( this.data.loading) {
            return;
        }
        wx.showLoading({
            title: '加载中...',
        })
        app.http.get('member/my-keep', ).then(res => {
            this.setData({
                keepList:res.data
               
            });
            wx.stopPullDownRefresh()
        })
        wx.hideLoading()
    },
    setStatus(e) {
        this.setData({
            status: e.currentTarget.dataset.status
        })
        if (this.data.status == 1 && !this.data.list.length) {
            this.loadAllStore();
        }
        if (this.data.status == 2) {
            this.loadKeepStore()
        }
    },
    onShow: function () {
        wx.setNavigationBarTitle({
            title: wx.getStorageSync('store'),
        })
    },
    /**上拉加载 */
    onReachBottom: function () {
        if (this.data.status == 1) {
            this.loadAllStore();
        } else {
            // this.loadKeepStore()
        }
    },
    /**刷新 */
    onPullDownRefresh: function () {
        const { status } = this.data
        if (status == 1) {
            this.setData({
                page: 1,
                total: 2,
                list: [],
                loading: false
            })
            this.loadAllStore();
        } else{
            this.setData({
                keepList:[]
            })
            this.loadKeepStore();
        }
    },
    /**
     * 分享
     */
    onShareAppMessage: function (res) {
        return {
            title: '',
            path: '/pages/home/home',
        }
    }
})